package com.woniuxy.controller;

import com.woniuxy.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.validation.Valid;

/**
 * @Author: ideaAdmin
 * TODO:
 * Date: 2022/2/10:11:26
 * Description:
 */

@Controller
@RequestMapping("/user")
public class UserController {


    @RequestMapping("/test")
    @ResponseBody
    public String setUser(@Valid @RequestBody User user, BindingResult bindingResult) {

        String msg = null;
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(user);
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            assert fieldError != null;
            modelAndView.addObject("fieldError",
                    fieldError.getField() +"" + fieldError.getDefaultMessage());
            msg = fieldError.getField() +"" + fieldError.getDefaultMessage();
        }else {
            msg = "/WEB-INF/jsp/ok.jsp";
        }

        modelAndView.setViewName("/WEB-INF/jsp/ok.jsp");


        return msg;
    }
}
